// 处理高德地图定位返回的数据,与后台不一致的问题

import {
  deepClone, isEmpty
} from "../newUtil"

/**
 * 重新修改高德地图定位返回的信息
 * @param {*} data 
 */
let cityFormat = (data) => {
  let originData = deepClone(data)
  // 直辖市
  let municipality = ['北京', '天津', '上海', '重庆']
  // 重庆的市辖区
  let chongqinCity = [
    "万州区",
    "涪陵区",
    "渝中区",
    "大渡口区",
    "江北区",
    "沙坪坝区",
    "九龙坡区",
    "南岸区",
    "北碚区",
    "綦江区",
    "大足区",
    "渝北区",
    "巴南区",
    "黔江区",
    "长寿区",
    "江津区",
    "合川区",
    "永川区",
    "南川区",
    "璧山区",
    "铜梁区",
    "潼南区",
    "荣昌区",
    "开州区",
    "梁平区",
    "武隆区"
  ]
  try {
    let address = data[0].regeocodeData.addressComponent
    let {
      province,
      city,
      district,
    } = address
    if (isEmpty(city)) {
      // 直辖市没有市级数据时默认的市一级改为 市辖区
      province = province.replace('市', '')
      if (municipality.includes(province)) {
        data[0].regeocodeData.addressComponent.city = '市辖区'
      }
      // 重庆市的需要特殊处理判断为市辖区还是县辖区
      if (province == '重庆' && !chongqinCity.includes(district)) {
        data[0].regeocodeData.addressComponent.city = '县辖区'
      }
    }
    return data
  } catch (e) {
    return originData
  }
}

export {
  cityFormat
}